Feature = class('Feature')

function Feature:initialize(name, image, x, y, rotation, scale_x, scale_y, offset_x, offset_y, t, visible )

	self.name = name or "Default Feature"
	self.image = image
	self.x = x or 0
	self.y = y or 0
	self.rotation = rotation or 0
	self.scale_x = scale_x or scale
	self.scale_y = scale_y or scale
	self.offset_x = offset_x or 0
	self.offset_y = offset_y or 0
	self.t = t or "feature"
	self.visible = visible or true
	self.msg_once = true
	self.y_index = y
	self.x_index = x


	self.lit = false

	if not features then
		features = {}
	end

	if not features[y] then
		print("features y " .. y)
		features[y] = {}
	end

	features[y][x] = self


end

function Feature:set_lit_all( bLit )
	if bLit ~= nil then
		for y = 1, #features do
			if features[y] then
				for x = 1, map_w do
					if features[y][x] then
						features[y][x].lit = bLit
					end
				end
			end
		end
	else
		print("FEATURE set bLit error no bLit")
	end
end

function Feature:update()

	if self.x == pX and self.y == pY then -- is the player on the feature?

		self:use() -- use dat bitch

	end

	if self.x ~= pX and self.y ~= pY then -- is the player off the feature?

		self.msg_once = true -- renew message ability

	end

end

function Feature:use()

	--print("default feature use")

	if self.text then

		self:draw_text() -- if you have text, draw it

	end

end

function Feature:get_by_name( name )

	for y = 1, map_h do
		if features[y] then
			for x = 1, map_w do
				if features[y][x] then
					if features[y][x].name == name then
						return features[y][x]
					end
				end
			end
		end
	end

end


function Feature:set_text(text)
	-- makes your self.text da text
	if text then
		self.text = text
	else
		print("text not set : ", text)
	end

end

function Feature:draw_text(text)

	-- make texts
	-- self.msg_once stops the infinite repeat

	self.text = self.text or text
	if self.msg_once then
		draw_text(self.text)
		self.msg_once = false
	end

end

require('src.features.battery')
require('src.features.bloodsplatter')
require('src.features.door')
require('src.features.elevator')
require('src.features.hazard')
require('src.features.med_analyzer')
require('src.features.scourge')
require('src.features.tablet')

function light_features()

	
	if player.light == true then
		for y = 1, map_h do

			if features[y] then

				for x = 1, map_w do

					if features[y][x] then
						-- if the light is on and collides with a feature
						if p_light_beam_collide(features[y][x].x+tile_size*scale,
							features[y][x].y+tile_size*scale) then

							local r = additive_range(player_x, player_y,
								features[y][x].x, features[y][x].y)
							--print (" r :", r)

							if r < (3*(tile_size*scale)) then

								features[y][x].lit = true -- set feature lit
								if features[y][x].t == "scourge" then

									features[y][x]:research() -- research if scourge



								end
							else
								if not SceneCompletelyLit then
									features[y][x].lit = false
								end
							end

						else
							if not SceneCompletelyLit then
								features[y][x].lit = false
							end
						end

					end

				end

			end

		end
	end
	
end

function feature_collider()

	-- is feature colliding with player?

	local pX = map_x+view_center_x
	local pY = map_y+view_center_y
	if features[pY] then
		if features[pY][pX] then
			return true
		else
			return false
		end
	else
		return false
	end

end

function feat_col(x, y)

	-- is feature colliding with x, y?

	if features[y] then
		if features[y][x] then

			if pX == x and pY == y then
				return true

			end

		end

	end

	return false
end

function feature_updates()

	-- update dem features!

	for i = 1, #features do
		if features[i] then
			for j = 1, #features[i] do
				if features[i][j] then

					features[i][j]:update()


				end

			end

		end

	end

end

function reset_feats()

	-- not in use, meant to be used to reset features
	-- to play messages more than once,
	-- but also not on infinite loop

	local pX = map_x+view_center_x
	local pY = map_y+view_center_y


	for i = 1, #features do
		if features[i] then
			for j = 1, #features[i] do
				if features[i][j] then

					if  i ~= pY and j ~= pX then


							features[i][j].msg_once = true
					end

				end

			end

		end

	end

end


function load_tablet_text()

	-- a big ole archive of text for the game

	tablet_text = {
		[1] = "\"If you are reading this, you must head to the shuttle bay.\nA terrible accident has happened, the station may be dangerous.\"",
		[2] = "All equipment has been removed from \'Deck D: Medical Bay\' due to contamination.",

	}



end
